//
//  ViewController.swift
//  XAPageViewDemo
//
//  Created by XangAm on 2017/12/28.
//  Copyright © 2017年 Lan. All rights reserved.
//

import UIKit


fileprivate let kSectionPageCellIdentifier = "SectionPageCell"
class ViewController: UIViewController {
    fileprivate var pageView : XAPageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        setupUI()
    
    }

}

//MARK: - UI
extension ViewController{
    fileprivate func setup(){
        title = "首页"
        
    }
    fileprivate func setupUI() -> Void{
        let titles : [String] = ["趣玩","动感","信息云","数码科技"]
        var childVcs : [UIViewController] = [UIViewController]();
        for _ in 0..<titles.count{
            let vc  = UIViewController()
            childVcs.append(vc)
        }
        let style =  XAPageViewStyle()
        let sectionPageLayout = XASectionPageLayout()
        sectionPageLayout.cols = 5
        sectionPageLayout.rows = 5
        sectionPageLayout.minimumLineSpacing      = 10
        sectionPageLayout.minimumInteritemSpacing = 10
        sectionPageLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        sectionPageLayout.scrollDirection = .horizontal
        //
        pageView = XAPageView(frame: CGRect(x: 0, y: navMaxY, width: view.bounds.width, height: 300), style: style, titles: titles, dataSource: self, delegate:self , layout: sectionPageLayout)
        pageView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kSectionPageCellIdentifier)
        view.addSubview(pageView)
        
        
        
        //        pageView.backgroundColor = UIColor.red
        
    }
    
}


//MARK: - <XAPageViewDataSource>
extension ViewController : XAPageViewDataSource{
    func pageViewOnSectionPageNumOfSection() -> Int {
         return 4
    }
    func pageView(_ pageView: XAPageView, onSectionPageNumOfItemsInSection section: Int) -> Int {
        if section == 0{
            return 10
        }else if section == 1{
            return  29
        }else{
            return 58
        }
    }
    
    func pageView(_ pageView: XAPageView, onSectionPageCellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell =  pageView.dequeueReusableCell(withReuseIdentifier: kSectionPageCellIdentifier, for: indexPath)
            cell.contentView.backgroundColor = UIColor.rondomColor
            return cell
        
    }
}

//MARK: - <XAPageViewDelegate>
extension ViewController : XAPageViewDelegate{
    func pageView(_ pageView: XAPageView, onSectionPageDidSelectItemAt indexPath: IndexPath) {
        print(indexPath)
    }
}

